

package com.hazelcast.map.impl.recordstore;

import com.hazelcast.config.EventJournalConfig;
import com.hazelcast.internal.serialization.Data;
import com.hazelcast.internal.services.ObjectNamespace;
import com.hazelcast.map.impl.MapContainer;
import com.hazelcast.map.impl.journal.MapEventJournal;
import com.hazelcast.map.impl.record.Record;

import javax.annotation.Nonnull;

public class EventJournalWriterMutationObserver implements MutationObserver {

    private final int partitionId;
    private final MapEventJournal eventJournal;
    private final EventJournalConfig eventJournalConfig;
    private final ObjectNamespace objectNamespace;

    public EventJournalWriterMutationObserver(MapEventJournal eventJournal, MapContainer mapContainer, int partitionId) {
        this.eventJournal = eventJournal;
        this.partitionId = partitionId;
        this.eventJournalConfig = mapContainer.getEventJournalConfig();
        this.objectNamespace = mapContainer.getObjectNamespace();
    }

    @Override
    public void onPutRecord(@Nonnull Data key, Record record, Object oldValue, boolean backup) {
        eventJournal.writeAddEvent(eventJournalConfig, objectNamespace, partitionId, key, record.getValue());
    }


    @Override
    public void onUpdateRecord(@Nonnull Data key, @Nonnull Record record, Object oldValue, Object newValue, boolean backup) {
        eventJournal.writeUpdateEvent(eventJournalConfig, objectNamespace, partitionId, key, oldValue, newValue);
    }

    @Override
    public void onRemoveRecord(Data key, Record record, boolean backup) {
        eventJournal.writeRemoveEvent(eventJournalConfig, objectNamespace, partitionId, key, record.getValue());
    }

    @Override
    public void onEvictRecord(Data key, Record record, boolean backup) {
        eventJournal.writeEvictEvent(eventJournalConfig, objectNamespace, partitionId, key, record.getValue());
    }

    @Override
    public void onLoadRecord(@Nonnull Data key, @Nonnull Record record, boolean backup) {
        eventJournal.writeLoadEvent(eventJournalConfig, objectNamespace, partitionId, key, record.getValue());
    }

    @Override
    public void onDestroy(boolean isDuringShutdown, boolean internal) {
        if (!internal) {
            eventJournal.destroy(objectNamespace, partitionId);
        }
    }

    @Override
    public void onReplicationPutRecord(@Nonnull Data key, @Nonnull Record record, boolean populateIndex) {
        // NOP
    }

    @Override
    public void onClear() {
        // NOP
    }

    @Override
    public void onReset() {
        // NOP
    }
}
